﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace CRM.Database.Model
{
    public class CreditConditionsCollection : AbstractCollection
    {
        public List<CreditConditions> CreditConditionsList
        {
            get { return Entities.CreditConditions.ToList(); }
        }


        /// <summary>
        /// Обновление
        /// </summary>
        /// <param name="item">Обновлённый элемент</param>
        /// <param name="userLogin">Имя пользователя</param>
        /// <returns>Результат операции</returns>
        public new bool Update(object item, string userLogin)
        {
            var newObject = (CreditConditions) item;
            if (newObject == null)
                return false;


            var object4Update = GetItemByIdFromDatabase(newObject.GetKeyValue()) as CreditConditions;
            if (object4Update == null)
                return false;

            object4Update.IdCreditType = newObject.IdCreditType;
            object4Update.Amount = newObject.Amount;
            object4Update.IdUnit = newObject.IdUnit;
            object4Update.NumberOfDays = newObject.NumberOfDays;
            object4Update.StartDate = newObject.StartDate;
            object4Update.EndDate = newObject.EndDate;
            object4Update.Comments = newObject.Comments;
            object4Update.IdContract = newObject.IdContract;
            object4Update.IdLegalEntity = newObject.IdLegalEntity;


            return base.Update(object4Update, userLogin);
        }

        #region Overrides of AbstractCollection

        /// <summary>
        /// Получение списка элементов
        /// </summary>
        /// <returns>Список элементов</returns>
        public override List<object> GetList()
        {
            return new List<object>(CreditConditionsList);
        }

        /// <summary>
        /// Получение типа текущего элемента
        /// </summary>
        /// <returns></returns>
        protected override Type GetItemType()
        {
            return typeof (CreditConditions);
        }

        /// <summary>
        /// Получение статического списка для хранения объектов
        /// </summary>
        /// <returns></returns>
        public override object GetStaticCollection()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Получение элемента из коллекции по идентификатору
        /// </summary>
        /// <param name="itemId">Идентификатор</param>
        /// <returns>Элемент</returns>
        public override object GetItemById(int itemId)
        {
            return Entities.CreditConditions.Find(itemId);
        }

        /// <summary>
        /// Обнуление статической коллекции
        /// </summary>
        protected override void SetStaticListToNULL()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}